package Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: happyiu
 * Date: 2022-03-05
 * Time: 14:27
 * Description:
 */
public class TestDemo2 {

    public static void main1(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(123);

    }

    public static void main2(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();

        list1.add("hello");
        list1.add("world");
        System.out.println(list1);
        //[hello, world]
    }

    public static void main3(String[] args) {
        List<String> list =new ArrayList<>();

        List<String> list1 = new ArrayList<>();

        list1.add("hello");
        list1.add("world");

        List<String> list2 = new ArrayList<>(10);
        list2.add("hello");
        list2.add("world");


        List<String> list3 = new ArrayList<>(list);
        list3.add("hello");
        list3.add("world");
        System.out.println(list1);


    }

    public static void main5(String[] args) {
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("world");
        System.out.println(list2);
        System.out.println("================");
        for(int i = 0; i< list2.size();i++) {
            System.out.print (list2.get(i)+" ");
        }
        System.out.println();
        System.out.println("==================");
        for (String s : list2) {
            System.out.print(s+" ");
        }
        System.out.println();
        System.out.println("========迭代器打印==========");
        Iterator<String> it = list2.iterator();
        while (it.hasNext()) {
            System.out.print(it.next()+" ");
        }

        System.out.println();
        System.out.println("========迭代器List相关打印==========");
        ListIterator<String> it2 = list2.listIterator();
        while (it2.hasNext()) {
            System.out.print(it2.next()+" ");
        }
    }

    public static void main6(String[] args) {
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("world");
        for (int i = 0; i < list2.size(); i++) {
            System.out.print(list2.get(i) + " ");
        }
        System.out.println();
    }

    public static void main7(String[] args) {
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("world");
        for (String s : list2) {
            System.out.print(s+" ");
        }
        System.out.println();
    }

    public static void main8(String[] args) {
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("world");
        Iterator<String> it = list2.iterator();
        while (it.hasNext()) {
            System.out.print(it.next()+" ");
        }
    }


    public static void main9(String[] args) {
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("world");
        Iterator<String> it = list2.iterator();
        while (it.hasNext()) {
            System.out.print(it.next()+" ");
        }
        //打印hello world
    }

    public static void main10(String[] args) {
        System.out.println("========迭代器List相关打印==========");
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("world");
        ListIterator<String> it2 = list2.listIterator();
        while (it2.hasNext()) {
            System.out.print(it2.next()+" ");
        }
    }


    public static void main11(String[] args) {
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("world");

        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
    }

    public static void main(String[] args) {
        ArrayList<String> list2 = new ArrayList<>();
        //CopyOnWriteArrayList<String> list2 = new CopyOnWriteArrayList<>();
        list2.add("hello");
        list2.add("world");
        list2.add("haha");
        ListIterator<String> it = list2.listIterator();
        while (it.hasNext()) {
            String ret = it.next();//抛出异常
            if(ret.equals("world")) {
                list2.add("welcome");
            }else {
                System.out.print(ret + " ");
            }
        }
        System.out.println("=================");
        System.out.println(list2);
    }

    public static void main13(String[] args) {
        CopyOnWriteArrayList<String> list2 = new CopyOnWriteArrayList<>();
        list2.add("hello");
        list2.add("world");
        list2.add("haha");
        ListIterator<String> it = list2.listIterator();
        while (it.hasNext()) {
            String ret = it.next();//抛出异常
            if(ret.equals("world")) {
                list2.add("welcome");
            }else {
                System.out.print(ret + " ");
            }
        }
        System.out.println("=================");
        System.out.println(list2);
    }



}
